package com.soyotec.algorithm.enums;

import com.soyotec.algorithm.algorithm.process.AlgorithmRunner;
import com.soyotec.algorithm.algorithm.process.AlgorithmRunnerWF;
import com.soyotec.algorithm.algorithm.process.ProcessAlgorithmPluginAdapter;
import com.soyotec.algorithm.context.OptAlgorithmContext;
import com.soyotec.algorithm.util.OPTFileUtil;

import java.io.File;

public enum LanguageType implements ProcessAlgorithmPluginAdapter {
    /**
     * python语言开发，执行时不需要把files中文件copy搭配workDir
     */
    PYTHON_CORE(1, "python") {
        @Override
        public AlgorithmRunner getAlgorithmRunner(OptAlgorithmContext optAlgorithmContext) {
            return new AlgorithmRunner(optAlgorithmContext);
        }

        @Override
        public String[] getCommandLines(File mainFile, AlgorithmRunner algorithmRunner, OptAlgorithmContext optAlgorithmContext) {
            return new String[]{"python", mainFile.getAbsolutePath(), optAlgorithmContext.getWorkDir().getAbsolutePath() + File.separator};
        }

        @Override
        public void copyFileToExecDir(OptAlgorithmContext optAlgorithmContext) {

        }

        @Override
        public File getExecDirFile(OptAlgorithmContext optAlgorithmContext, String mainFileName) {
            File[] otherFiles = optAlgorithmContext.loadOtherFiles();
            if(otherFiles != null && otherFiles.length > 0){
                File file = otherFiles[0];
                return file.getParentFile();
            }
            return null;
        }

    },
    /**
     * matlab语言开发，执行时不需要把files中文件copy搭配workDir
     */
    MATLAB_CORE(2, "matlab") {
        @Override
        public AlgorithmRunner getAlgorithmRunner(OptAlgorithmContext optAlgorithmContext) {
            return new AlgorithmRunnerWF(optAlgorithmContext);
        }

        @Override
        public String[] getCommandLines(File mainFile, AlgorithmRunner algorithmRunner, OptAlgorithmContext optAlgorithmContext) {
            AlgorithmRunnerWF runner = (AlgorithmRunnerWF) algorithmRunner;
            String scriptDir = mainFile.getParentFile().getAbsolutePath();
            String command = "\" mmain('" + optAlgorithmContext.getWorkDir().getAbsolutePath() + File.separator
                    + "','" + scriptDir + File.separator + "','" + runner.getTempFile() + "')\"";
            return new String[]{"matlab", "-nosplash", "-nodesktop", "-minimize", "-noFigureWindows", "-wait", "-batch", command};
        }

        @Override
        public void copyFileToExecDir(OptAlgorithmContext optAlgorithmContext) {

        }

        @Override
        public File getExecDirFile(OptAlgorithmContext optAlgorithmContext, String mainFileName) {
            File[] otherFiles = optAlgorithmContext.loadOtherFiles();
            if(otherFiles != null && otherFiles.length > 0){
                File file = otherFiles[0];
                return file.getParentFile();
            }
            return null;
        }


    },
    /**
     * c++语言开发，未集成
     */
    CPP_CORE(3, "c++") {
        @Override
        public AlgorithmRunner getAlgorithmRunner(OptAlgorithmContext optAlgorithmContext) {
            return null;
        }

        @Override
        public String[] getCommandLines(File mainFile, AlgorithmRunner algorithmRunner, OptAlgorithmContext optAlgorithmContext) {
            return new String[0];
        }

        @Override
        public void copyFileToExecDir(OptAlgorithmContext optAlgorithmContext) {

        }

        @Override
        public File getExecDirFile(OptAlgorithmContext optAlgorithmContext, String mainFileName) {
            return null;
        }


    },
    /**
     * c语言开发，未集成
     */
    C_CORE(4, "c") {
        @Override
        public AlgorithmRunner getAlgorithmRunner(OptAlgorithmContext optAlgorithmContext) {
            return null;
        }

        @Override
        public String[] getCommandLines(File mainFile, AlgorithmRunner algorithmRunner, OptAlgorithmContext optAlgorithmContext) {
            return new String[0];
        }

        @Override
        public void copyFileToExecDir(OptAlgorithmContext optAlgorithmContext) {

        }

        @Override
        public File getExecDirFile(OptAlgorithmContext optAlgorithmContext, String mainFileName) {
            return null;
        }


    },
    /**
     * ocatve语言开发，执行时不需要把files中文件copy搭配workDir
     */
    OCATVE_CORE(5, "ocatve") {
        @Override
        public AlgorithmRunner getAlgorithmRunner(OptAlgorithmContext optAlgorithmContext) {
            return new AlgorithmRunner(optAlgorithmContext);
        }

        @Override
        public String[] getCommandLines(File mainFile, AlgorithmRunner algorithmRunner, OptAlgorithmContext optAlgorithmContext) {
            String scriptDir = mainFile.getParentFile().getAbsolutePath();
            return new String[]{"octave-cli", scriptDir, "-q ", "--silent", optAlgorithmContext.getWorkDir().getAbsolutePath() + File.separator, scriptDir + File.separator};
        }

        @Override
        public void copyFileToExecDir(OptAlgorithmContext optAlgorithmContext) {

        }

        @Override
        public File getExecDirFile(OptAlgorithmContext optAlgorithmContext, String mainFileName) {
            File[] otherFiles = optAlgorithmContext.loadOtherFiles();
            if(otherFiles != null && otherFiles.length > 0){
                File file = otherFiles[0];
                return file.getParentFile();
            }
            return null;
        }


    },
    /**
     * 未集成
     */
    DAKOTA_CORE(6, "dakota") {
        @Override
        public AlgorithmRunner getAlgorithmRunner(OptAlgorithmContext optAlgorithmContext) {
            return null;
        }

        @Override
        public String[] getCommandLines(File mainFile, AlgorithmRunner algorithmRunner, OptAlgorithmContext optAlgorithmContext) {
            return new String[0];
        }

        @Override
        public void copyFileToExecDir(OptAlgorithmContext optAlgorithmContext) {

        }

        @Override
        public File getExecDirFile(OptAlgorithmContext optAlgorithmContext, String mainFileName) {
            return null;
        }


    },
    /**
     * 未集成
     */
    MODEL_CENTER_CORE(7, "modelCenter") {
        @Override
        public AlgorithmRunner getAlgorithmRunner(OptAlgorithmContext optAlgorithmContext) {
            return null;
        }

        @Override
        public String[] getCommandLines(File mainFile, AlgorithmRunner algorithmRunner, OptAlgorithmContext optAlgorithmContext) {
            return new String[0];
        }

        @Override
        public void copyFileToExecDir(OptAlgorithmContext optAlgorithmContext) {

        }

        @Override
        public File getExecDirFile(OptAlgorithmContext optAlgorithmContext, String mainFileName) {
            return null;
        }


    },
    /**
     * python语言转换的exe，执行时不需要把files中文件copy搭配workDir
     */
    PYTHON_EXE(8, "python_exe") {
        @Override
        public AlgorithmRunner getAlgorithmRunner(OptAlgorithmContext optAlgorithmContext) {
            return new AlgorithmRunner(optAlgorithmContext);
        }

        @Override
        public String[] getCommandLines(File mainFile, AlgorithmRunner algorithmRunner, OptAlgorithmContext optAlgorithmContext) {
            return new String[]{mainFile.getAbsolutePath(), optAlgorithmContext.getWorkDir().getAbsolutePath() + File.separator};
        }

        @Override
        public void copyFileToExecDir(OptAlgorithmContext optAlgorithmContext) {

        }

        @Override
        public File getExecDirFile(OptAlgorithmContext optAlgorithmContext, String mainFileName) {
            File[] otherFiles = optAlgorithmContext.loadOtherFiles();
            if(otherFiles != null && otherFiles.length > 0){
                File file = otherFiles[0];
                return file.getParentFile();
            }
            return null;
        }


    },
    /**
     * matlab语言转换的exe，执行时不需要把files中文件copy搭配workDir
     */
    MATLAB_EXE(9, "matlab_exe") {
        @Override
        public AlgorithmRunner getAlgorithmRunner(OptAlgorithmContext optAlgorithmContext) {
            return new AlgorithmRunnerWF(optAlgorithmContext);
        }

        @Override
        public String[] getCommandLines(File mainFile, AlgorithmRunner algorithmRunner, OptAlgorithmContext optAlgorithmContext) {
            AlgorithmRunnerWF runner = (AlgorithmRunnerWF) algorithmRunner;
            return new String[]{mainFile.getAbsolutePath(), optAlgorithmContext.getWorkDir().getAbsolutePath() + File.separator, optAlgorithmContext.getWorkDir().getAbsolutePath() + File.separator, runner.getTempFile()};
        }

        @Override
        public void copyFileToExecDir(OptAlgorithmContext optAlgorithmContext) {

        }

        @Override
        public File getExecDirFile(OptAlgorithmContext optAlgorithmContext, String mainFileName) {
            File[] otherFiles = optAlgorithmContext.loadOtherFiles();
            if(otherFiles != null && otherFiles.length > 0){
                File file = otherFiles[0];
                return file.getParentFile();
            }
            return null;
        }


    },
    /**
     * c++语言转换的exe，未集成
     */
    CPP_EXE(10, "c++_exe") {
        @Override
        public AlgorithmRunner getAlgorithmRunner(OptAlgorithmContext optAlgorithmContext) {
            return null;
        }

        @Override
        public String[] getCommandLines(File mainFile, AlgorithmRunner algorithmRunner, OptAlgorithmContext optAlgorithmContext) {
            return new String[0];
        }

        @Override
        public void copyFileToExecDir(OptAlgorithmContext optAlgorithmContext) {

        }

        @Override
        public File getExecDirFile(OptAlgorithmContext optAlgorithmContext, String mainFileName) {
            return null;
        }

    },
    /**
     * c语言转换的exe，未集成
     */
    C_EXE(11, "c_exe") {
        @Override
        public AlgorithmRunner getAlgorithmRunner(OptAlgorithmContext optAlgorithmContext) {
            return null;
        }

        @Override
        public String[] getCommandLines(File mainFile, AlgorithmRunner algorithmRunner, OptAlgorithmContext optAlgorithmContext) {
            return new String[0];
        }

        @Override
        public void copyFileToExecDir(OptAlgorithmContext optAlgorithmContext) {

        }

        @Override
        public File getExecDirFile(OptAlgorithmContext optAlgorithmContext, String mainFileName) {
            return null;
        }
    },
    /**
     * octave语言转换的exe，未集成
     */
    OCATVE_EXE(12, "octave_exe") {
        @Override
        public AlgorithmRunner getAlgorithmRunner(OptAlgorithmContext optAlgorithmContext) {
            return null;
        }

        @Override
        public String[] getCommandLines(File mainFile, AlgorithmRunner algorithmRunner, OptAlgorithmContext optAlgorithmContext) {
            return new String[0];
        }

        @Override
        public void copyFileToExecDir(OptAlgorithmContext optAlgorithmContext) {

        }

        @Override
        public File getExecDirFile(OptAlgorithmContext optAlgorithmContext, String mainFileName) {
            return null;
        }

    },
    /**
     * dakota软件转换的exe，执行时需要把files中文件copy搭配workDir
     */
    DAKOTA_EXE(13, "dakota_exe") {
        @Override
        public AlgorithmRunner getAlgorithmRunner(OptAlgorithmContext optAlgorithmContext) {
            return new AlgorithmRunner(optAlgorithmContext);
        }

        @Override
        public String[] getCommandLines(File mainFile, AlgorithmRunner algorithmRunner, OptAlgorithmContext optAlgorithmContext) {
            File copyMainFile = OPTFileUtil.findMainFile(optAlgorithmContext.getWorkDir(),mainFile.getName());
            return new String[]{copyMainFile.getAbsolutePath(),copyMainFile.getParentFile().getAbsolutePath()};
        }

        @Override
        public void copyFileToExecDir(OptAlgorithmContext optAlgorithmContext) {
            long startTime = System.currentTimeMillis();
            File[] othersFile = optAlgorithmContext.loadOtherFiles();
            if(othersFile != null && othersFile.length > 0){
                OPTFileUtil.copyFilesToDir(othersFile,optAlgorithmContext.getWorkDir());
            }
            long endTime = System.currentTimeMillis();
            System.out.println("copy文件耗时：" + (endTime - startTime) / 1000 + "秒");
        }

        @Override
        public File getExecDirFile(OptAlgorithmContext optAlgorithmContext, String mainFileName) {
//            return new File("E:\\software\\dakota\\dakota-6.19.0\\bin");
            return optAlgorithmContext.getWorkDir();
        }


    },
    /**
     * 未集成
     */
    MODEL_CENTER_EXE(14, "modelCenter_exe") {
        @Override
        public AlgorithmRunner getAlgorithmRunner(OptAlgorithmContext optAlgorithmContext) {
            return null;
        }

        @Override
        public String[] getCommandLines(File mainFile, AlgorithmRunner algorithmRunner, OptAlgorithmContext optAlgorithmContext) {
            return new String[0];
        }

        @Override
        public void copyFileToExecDir(OptAlgorithmContext optAlgorithmContext) {

        }

        @Override
        public File getExecDirFile(OptAlgorithmContext optAlgorithmContext, String mainFileName) {
            return null;
        }

    },

    ;


    private Integer code;
    private String title;

    private LanguageType(Integer code, String title) {
        this.code = code;
        this.title = title;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public static LanguageType findByTitle(String title) {
        switch (title) {
            case "python":
                return PYTHON_CORE;
            case "matlab":
                return MATLAB_CORE;
            case "c++":
                return CPP_CORE;
            case "c":
                return C_CORE;
            case "ocatve":
                return OCATVE_CORE;
            case "dakota":
                return DAKOTA_CORE;
            case "modelCenter":
                return MODEL_CENTER_CORE;
            case "python_exe":
                return PYTHON_EXE;
            case "matlab_exe":
                return MATLAB_EXE;
            case "c++_exe":
                return CPP_EXE;
            case "c_exe":
                return C_EXE;
            case "ocatve_exe":
                return OCATVE_EXE;
            case "dakota_exe":
                return DAKOTA_EXE;
            case "modelCenter_exe":
                return MODEL_CENTER_EXE;
            default:
                throw new RuntimeException("指定的执行模式暂不支持：" + title);
        }
    }

}
